﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Ninject.Parameters;
using FactoryOfSessions;
using RepositoriesInterfaces;
using Repositories;
using NHibernate;

namespace Subfaculty
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            // ----------- Маршруты для Ajax-функций -----------------------------------------------------
            routes.MapRoute(
                "LoadDistributionAtom",
                "LoadDistribution/ajaxDistributeAtom/{teacherId}/{atomId}",
                new { controller = "LoadDistribution", action = "ajaxDistributeAtom" }
            );
            routes.MapRoute(
                "LoadDistributionDel",
                "LoadDistribution/ajaxDistributedAtomDelete/{teacherId}/{atomId}",
                new { controller = "LoadDistribution", action = "ajaxDistributedAtomDelete" }
            );
            routes.MapRoute(
                "LoadDistributionLoadType",
                "LoadDistribution/ajaxDistributeLoadType/{teacherId}/{disciplineId}/{_loadType}",
                new { controller = "LoadDistribution", action = "ajaxDistributeLoadType" }
            );
            routes.MapRoute(
                "LoadDistributionDelLoadType",
                "LoadDistribution/ajaxDistributedLoadTypeDelete/{teacherId}/{disciplineId}/{_loadType}",
                new { controller = "LoadDistribution", action = "ajaxDistributedLoadTypeDelete" }
            );
            routes.MapRoute(
                "LoadDistributionLoadTypeDiscipline",
                "LoadDistribution/ajaxDistributeLoadDiscipline/{teacherId}/{disciplineId}",
                new { controller = "LoadDistribution", action = "ajaxDistributeLoadDiscipline" }
            );
            routes.MapRoute(
                "LoadDistributionLoadDiscipline",
                "LoadDistribution/ajaxDistributedLoadByDisciplineDelete/{teacherId}/{disciplineId}",
                new { controller = "LoadDistribution", action = "ajaxDistributedLoadByDisciplineDelete" }
            );
            routes.MapRoute(
                "DivideAtoms",
                "LoadDistribution/ajaxDivideLoadAtoms/{firstAtomHours}/{secondAtomHours}/{atomId}",
                new { controller = "LoadDistribution", action = "ajaxDivideLoadAtoms" }
            );

            //---------------------------------------------------------------------------------------------
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "StudyPlan", action = "Index", id = 0 } // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            InitDependences();
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        private void InitDependences()
        {
            SessionsFactory.Init(Resources.ServerName, Resources.DBName);
            IKernel kernel = new StandardKernel();
            BindDependences(kernel);

            DependencyResolver.SetResolver(new MyDependencyResolver(kernel));
        }

        private void BindDependences(IKernel kernel)
        {
            kernel.Bind<ISession>().ToConstant(SessionsFactory.GetSession());
            kernel.Bind<IDisciplineRepository>().To<DisciplineRepository>();
            kernel.Bind<IDirectionRepository>().To<DirectionRepository>();
            kernel.Bind<IGroupRepository>().To<GroupRepository>();
            kernel.Bind<IAllocationRepository>().To<AllocationRepository>();
            kernel.Bind<IDepartmentRepository>().To<DepartmentRepository>();
            kernel.Bind<ILoadAtomRepository>().To<LoadAtomRepository>();
            kernel.Bind<IStudyPlanItemRepository>().To<StudyPlanItemRepository>();
            kernel.Bind<ITeacherRepository>().To<TeacherRepository>();
        }
    }

    public class MyDependencyResolver : IDependencyResolver
    {
        IKernel _kernel;
        public MyDependencyResolver(IKernel kernel)
        {
            _kernel = kernel;
        }

        public object GetService(Type serviceType)
        {
            return _kernel.TryGet(serviceType, new IParameter[0]);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _kernel.GetAll(serviceType, new IParameter[0]);
        }
    }
}